home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / xml2po / docbook.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.4 KB  |  196 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import libxml2
  6. import os
  7. import md5
  8. import sys
  9.  
  10. class docbookXmlMode:
  11.     '''Class for special handling of DocBook document types.
  12.  
  13.     It sets lang attribute on article elements, and adds translators
  14.     to articleinfo/copyright.'''
  15.     
  16.     def __init__(self):
  17.         self.lists = [
  18.             'itemizedlist',
  19.             'orderedlist',
  20.             'variablelist',
  21.             'segmentedlist',
  22.             'simplelist',
  23.             'calloutlist',
  24.             'varlistentry']
  25.         self.objects = [
  26.             'figure',
  27.             'textobject',
  28.             'imageobject',
  29.             'mediaobject',
  30.             'screenshot']
  31.  
  32.     
  33.     def getIgnoredTags(self):
  34.         '''Returns array of tags to be ignored.'''
  35.         return self.objects + self.lists
  36.  
  37.     
  38.     def getFinalTags(self):
  39.         """Returns array of tags to be considered 'final'."""
  40.         return [
  41.             'para',
  42.             'formalpara',
  43.             'simpara',
  44.             'releaseinfo',
  45.             'revnumber',
  46.             'title',
  47.             'date',
  48.             'term',
  49.             'programlisting'] + self.objects + self.lists
  50.  
  51.     
  52.     def getSpacePreserveTags(self):
  53.         '''Returns array of tags in which spaces are to be preserved.'''
  54.         return [
  55.             'classsynopsisinfo',
  56.             'computeroutput',
  57.             'funcsynopsisinfo',
  58.             'literallayout',
  59.             'programlisting',
  60.             'screen',
  61.             'synopsis',
  62.             'userinput']
  63.  
  64.     
  65.     def getTreatedAttributes(self):
  66.         '''Returns array of tag attributes which content is to be translated'''
  67.         return []
  68.  
  69.     
  70.     def getStringForTranslators(self):
  71.         '''Returns string which will be used to credit translators.'''
  72.         return 'translator-credits'
  73.  
  74.     
  75.     def getCommentForTranslators(self):
  76.         '''Returns a comment to be added next to string for crediting translators.'''
  77.         return 'Put one translator per line, in the form of NAME <EMAIL>, YEAR1, YEAR2.'
  78.  
  79.     
  80.     def _find_articleinfo(self, node):
  81.         if node.name == 'articleinfo' or node.name == 'bookinfo':
  82.             return node
  83.         child = node.children
  84.         while child:
  85.             ret = self._find_articleinfo(child)
  86.             if ret:
  87.                 return ret
  88.             child = child.next
  89.             continue
  90.             ret
  91.  
  92.     
  93.     def _find_lastcopyright(self, node):
  94.         if not node.children:
  95.             return None
  96.         last = node.lastChild()
  97.         tmp = last
  98.         while tmp:
  99.             if tmp.name == 'copyright':
  100.                 last = tmp
  101.                 break
  102.             
  103.             tmp = tmp.prev
  104.         return last
  105.  
  106.     
  107.     def _md5_for_file(self, filename):
  108.         hash = md5.new()
  109.         input = open(filename, 'rb')
  110.         read = input.read(4096)
  111.         while read:
  112.             hash.update(read)
  113.             read = input.read(4096)
  114.         input.close()
  115.         return hash.hexdigest()
  116.  
  117.     
  118.     def _output_images(self, node, msg):
  119.         if node and node.type == 'element' and node.name == 'imagedata':
  120.             attr = node.prop('fileref')
  121.             if attr:
  122.                 dir = os.path.dirname(msg.filename)
  123.                 fullpath = os.path.join(dir, attr)
  124.                 if os.path.exists(fullpath):
  125.                     hash = self._md5_for_file(fullpath)
  126.                 else:
  127.                     hash = "THIS FILE DOESN'T EXIST"
  128.                     print >>sys.stderr, "Warning: image file '%s' not found." % fullpath
  129.                 msg.outputMessage("@@image: '%s'; md5=%s" % (attr, hash), node.lineNo(), 'When image changes, this message will be marked fuzzy or untranslated for you.\n' + "It doesn't matter what you translate it to: it's not used at all.")
  130.             
  131.         elif node and node.children:
  132.             child = node.children
  133.             while child:
  134.                 self._output_images(child, msg)
  135.                 child = child.next
  136.         
  137.  
  138.     
  139.     def preProcessXml(self, doc, msg):
  140.         '''Add additional messages of interest here.'''
  141.         root = doc.getRootElement()
  142.         self._output_images(root, msg)
  143.  
  144.     
  145.     def postProcessXmlTranslation(self, doc, language, translators):
  146.         '''Sets a language and translators in "doc" tree.
  147.         
  148.         "translators" is a string consisted of "Name <email>, years" pairs
  149.         of each translator, separated by newlines.'''
  150.         root = doc.getRootElement()
  151.         while root and root.name != 'article' and root.name != 'book':
  152.             root = root.next
  153.         if root:
  154.             if root.name == 'article' or root.name == 'book':
  155.                 root.setProp('lang', language)
  156.             else:
  157.                 return None
  158.             if root.name == 'book' == self.getStringForTranslators():
  159.                 return None
  160.             if translators:
  161.                 ai = self._find_articleinfo(root)
  162.                 if not ai:
  163.                     return None
  164.                 lines = translators.split('\n')
  165.                 for line in lines:
  166.                     line = line.strip()
  167.                     match = re.match('^([^<,]+)\\s*(?:<([^>,]+)>)?,\\s*(.*)$', line)
  168.                     if match:
  169.                         last = self._find_lastcopyright(ai)
  170.                         copy = libxml2.newNode('copyright')
  171.                         if match.group(3):
  172.                             copy.newChild(None, 'year', match.group(3).encode('utf-8'))
  173.                         
  174.                         if match.group(1) and match.group(2):
  175.                             holder = match.group(1) + '(%s)' % match.group(2)
  176.                         elif match.group(1):
  177.                             holder = match.group(1)
  178.                         elif match.group(2):
  179.                             holder = match.group(2)
  180.                         else:
  181.                             holder = '???'
  182.                         copy.newChild(None, 'holder', holder.encode('utf-8'))
  183.                         continue
  184.                 
  185.             
  186.  
  187.  
  188. if __name__ == '__main__':
  189.     test = docbookXmlMode()
  190.     print 'Ignored tags       : ' + repr(test.getIgnoredTags())
  191.     print 'Final tags         : ' + repr(test.getFinalTags())
  192.     print 'Space-preserve tags: ' + repr(test.getSpacePreserveTags())
  193.     print "Credits from string: '%s'" % test.getStringForTranslators()
  194.     print "Explanation for credits:\n\t'%s'" % test.getCommentForTranslators()
  195.  
  196.